using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using Publicuse.Entity;
using Publicuse.Util;
using Publicuse.Util;
using Publicuse.Util.Excel;
using ServiceStack.Script;

namespace Publicuse.Export
{
  /// <summary>
  /// 用户信息辅助类
  /// </summary>
  public class UserExport
  {
    /// <summary>
    /// 导出信息设置
    /// </summary>
    /// <param name="fileName"></param>
    /// <param name="excelName"></param>
    /// <param name="list"></param>
    /// <param name="path"></param>
    /// <param name="msg"></param>
    /// <returns></returns>
    public static MemoryStream ExportData(
      string fileName,
      string excelName,
      List<User> list,
      string path,
      out string msg
    )
    {
      msg = string.Empty;
      MemoryStream ms = ExcelUtil.GetExcelUtil.InitExportData(
        fileName,
        excelName,
        list,
        (row, cell, sheet1, TextSytle, srow, hssfworkbook) =>
        {
          foreach (User item in list)
          {
            srow++;
            row = sheet1.CreateRow(srow);

            //账号
            cell = row.CreateCell(0);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.uCode);

            //名称
            cell = row.CreateCell(1);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.uName);

            //角色名称
            cell = row.CreateCell(2);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.rName);

            //状态
            cell = row.CreateCell(3);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.status == 1 ? "正常" : "停用");

            //身份证
            cell = row.CreateCell(4);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.uIdCardNo);

            //手机号
            cell = row.CreateCell(5);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.uMobile);

            //登录时间
            cell = row.CreateCell(6);
            cell.CellStyle = TextSytle;
            if (!string.IsNullOrEmpty(item.logintime))
            {
              cell.SetCellValue(Convert.ToDateTime(item.logintime).ToString("yyyy-MM-dd HH:mm:ss"));
            }

            //创建时间
            cell = row.CreateCell(7);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.cTime.ToString("yyyy-MM-dd HH:mm:ss"));

            //修改密码时间
            cell = row.CreateCell(8);
            cell.CellStyle = TextSytle;
            if (!string.IsNullOrEmpty(item.lasttimeupw))
            {
              cell.SetCellValue(
                Convert.ToDateTime(item.lasttimeupw).ToString("yyyy-MM-dd HH:mm:ss")
              );
            }

            //密码
            //cell = row.CreateCell(9);
            //cell.CellStyle = TextSytle;
            //cell.SetCellValue(item.uPwd);

            ////头像
            //cell = row.CreateCell(11);
            //cell.CellStyle = TextSytle;
            //cell.SetCellValue(string.Empty);
            //if (!string.IsNullOrEmpty(item.uploadyhead))
            //{
            //    row.HeightInPoints = 100;
            //    sheet1.AddImageToCell(hssfworkbook, path, item.uploadyhead, srow, 11, out var msgb);
            //}
          }
        },
        out msg,
        2
      );
      return ms;
    }

    /// <summary>
    /// 用户角色资源企业导出数据设置
    /// </summary>
    /// <param name="excelName"></param>
    /// <param name="fileName"></param>
    /// <param name="list"></param>
    /// <param name="msg"></param>
    /// <returns></returns>
    public static MemoryStream ExportDatab(
      string fileName,
      string excelName,
      List<UserRoleCompany> list,
      out string msg
    )
    {
      msg = string.Empty;
      MemoryStream ms = ExcelUtil.GetExcelUtil.InitExportData(
        fileName,
        excelName,
        list,
        (row, cell, sheet1, TextSytle, srow, hssfworkbook) =>
        {
          var sIndex = 1;
          var istwo = false;
          var isthree = false;
          foreach (UserRoleCompany item in list)
          {
            srow++;
            row = sheet1.CreateRow(srow);
            row.Height = 7 * 200; //设置高度

            if (item.uType == 2 && !istwo)
            {
              row.Height = 2 * 200; //设置高度
              istwo = true;
              var yystyle = sheet1.GetRow(3).Cells.FirstOrDefault().CellStyle;
              cell = row.CreateCell(0);
              cell.CellStyle = yystyle;
              cell.SetCellValue("二、企业上报角色");
              cell = row.CreateCell(1);
              cell.CellStyle = yystyle;
              cell.SetCellValue("二、企业上报角色");
              cell = row.CreateCell(2);
              cell.CellStyle = yystyle;
              cell.SetCellValue("二、企业上报角色");
              cell = row.CreateCell(3);
              cell.CellStyle = yystyle;
              cell.SetCellValue("二、企业上报角色");
              cell = row.CreateCell(4);
              cell.CellStyle = yystyle;
              cell.SetCellValue("二、企业上报角色");
              cell = row.CreateCell(5);
              cell.CellStyle = yystyle;
              cell.SetCellValue("二、企业上报角色");
              cell = row.CreateCell(6);
              cell.CellStyle = yystyle;
              cell.SetCellValue("二、企业上报角色");
              sheet1.AddMergedRegion(new CellRangeAddress(srow, srow, 0, 6)); //起始行，结束行，起始列，结束列
              srow++;
              row = sheet1.CreateRow(srow);
            }
            if (item.uType == 3 && !isthree)
            {
              row.Height = 2 * 200; //设置高度
              isthree = true;
              var yystyle = sheet1.GetRow(3).Cells.FirstOrDefault().CellStyle;
              cell = row.CreateCell(0);
              cell.CellStyle = yystyle;
              cell.SetCellValue("三、其他角色");
              cell = row.CreateCell(1);
              cell.CellStyle = yystyle;
              cell.SetCellValue("三、其他角色");
              cell = row.CreateCell(2);
              cell.CellStyle = yystyle;
              cell.SetCellValue("三、其他角色");
              cell = row.CreateCell(3);
              cell.CellStyle = yystyle;
              cell.SetCellValue("三、其他角色");
              cell = row.CreateCell(4);
              cell.CellStyle = yystyle;
              cell.SetCellValue("三、其他角色");
              cell = row.CreateCell(5);
              cell.CellStyle = yystyle;
              cell.SetCellValue("三、其他角色");
              cell = row.CreateCell(6);
              cell.CellStyle = yystyle;
              cell.SetCellValue("三、其他角色");
              sheet1.AddMergedRegion(new CellRangeAddress(srow, srow, 0, 6)); //起始行，结束行，起始列，结束列
              srow++;
              row = sheet1.CreateRow(srow);
            }

            //序号
            cell = row.CreateCell(0);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(sIndex);

            //名称
            cell = row.CreateCell(1);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.uname);

            //用户
            cell = row.CreateCell(2);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.ucode);

            //密码
            cell = row.CreateCell(3);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.upass);

            //所属角色
            cell = row.CreateCell(4);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.rname);

            //开通功能的权限信息
            cell = row.CreateCell(5);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.resourcelist);

            //开通企业权限信息
            cell = row.CreateCell(6);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.companylist);

            sIndex++;
          }
        },
        out msg,
        3,
        true
      );
      return ms;
    }

    /// <summary>
    /// 用户角色资源导出数据设置
    /// </summary>
    /// <param name="excelName"></param>
    /// <param name="fileName"></param>
    /// <param name="list"></param>
    /// <param name="msg"></param>
    /// <returns></returns>
    public static MemoryStream ExportDatac(
      string fileName,
      string excelName,
      List<UserRoleCompany> list,
      out string msg
    )
    {
      msg = string.Empty;
      MemoryStream ms = ExcelUtil.GetExcelUtil.InitExportData(
        fileName,
        excelName,
        list,
        (row, cell, sheet1, TextSytle, srow, hssfworkbook) =>
        {
          var sIndex = 1;
          var istwo = false;
          var isthree = false;
          foreach (UserRoleCompany item in list)
          {
            srow++;
            row = sheet1.CreateRow(srow);
            row.Height = 7 * 200; //设置高度

            if (item.uType == 2 && !istwo)
            {
              row.Height = 2 * 200; //设置高度
              istwo = true;
              var yystyle = sheet1.GetRow(3).Cells.FirstOrDefault().CellStyle;
              cell = row.CreateCell(0);
              cell.CellStyle = yystyle;
              cell.SetCellValue("二、企业上报角色");
              cell = row.CreateCell(1);
              cell.CellStyle = yystyle;
              cell.SetCellValue("二、企业上报角色");
              cell = row.CreateCell(2);
              cell.CellStyle = yystyle;
              cell.SetCellValue("二、企业上报角色");
              cell = row.CreateCell(3);
              cell.CellStyle = yystyle;
              cell.SetCellValue("二、企业上报角色");
              cell = row.CreateCell(4);
              cell.CellStyle = yystyle;
              cell.SetCellValue("二、企业上报角色");
              cell = row.CreateCell(5);
              cell.CellStyle = yystyle;
              cell.SetCellValue("二、企业上报角色");
              cell = row.CreateCell(6);
              cell.CellStyle = yystyle;
              cell.SetCellValue("二、企业上报角色");
              sheet1.AddMergedRegion(new CellRangeAddress(srow, srow, 0, 6)); //起始行，结束行，起始列，结束列
              srow++;
              row = sheet1.CreateRow(srow);
            }
            if (item.uType == 3 && !isthree)
            {
              row.Height = 2 * 200; //设置高度
              isthree = true;
              var yystyle = sheet1.GetRow(3).Cells.FirstOrDefault().CellStyle;
              cell = row.CreateCell(0);
              cell.CellStyle = yystyle;
              cell.SetCellValue("三、其他角色");
              cell = row.CreateCell(1);
              cell.CellStyle = yystyle;
              cell.SetCellValue("三、其他角色");
              cell = row.CreateCell(2);
              cell.CellStyle = yystyle;
              cell.SetCellValue("三、其他角色");
              cell = row.CreateCell(3);
              cell.CellStyle = yystyle;
              cell.SetCellValue("三、其他角色");
              cell = row.CreateCell(4);
              cell.CellStyle = yystyle;
              cell.SetCellValue("三、其他角色");
              cell = row.CreateCell(5);
              cell.CellStyle = yystyle;
              cell.SetCellValue("三、其他角色");
              cell = row.CreateCell(6);
              cell.CellStyle = yystyle;
              cell.SetCellValue("三、其他角色");
              sheet1.AddMergedRegion(new CellRangeAddress(srow, srow, 0, 6)); //起始行，结束行，起始列，结束列
              srow++;
              row = sheet1.CreateRow(srow);
            }
            row.Height = 2 * 140; //设置高度
            //序号
            cell = row.CreateCell(0);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(sIndex);
            //名称
            cell = row.CreateCell(1);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.uname);
            //用户
            cell = row.CreateCell(2);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.ucode);
            //密码
            cell = row.CreateCell(3);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.upass);
            //线路统计(月度)管理
            cell = row.CreateCell(4);
            cell.CellStyle = TextSytle;
            if (item.authoritys.Any(o => o.Contains("线路统计(月度)管理")))
            {
              cell.SetCellValue("√");
            }
            else
            {
              cell.SetCellValue("");
            }
            //车辆统计(月度)管理
            cell = row.CreateCell(5);
            cell.CellStyle = TextSytle;
            if (item.authoritys.Any(o => o.Contains("车辆统计(月度)管理")))
            {
              cell.SetCellValue("√");
            }
            else
            {
              cell.SetCellValue("");
            }
            //首末站统计(月度)管理
            cell = row.CreateCell(6);
            cell.CellStyle = TextSytle;
            if (item.authoritys.Any(o => o.Contains("首末站统计(月度)管理")))
            {
              cell.SetCellValue("√");
            }
            else
            {
              cell.SetCellValue("");
            }
            //中途站统计(月度)管理
            cell = row.CreateCell(7);
            cell.CellStyle = TextSytle;
            if (item.authoritys.Any(o => o.Contains("中途站统计(月度)管理")))
            {
              cell.SetCellValue("√");
            }
            else
            {
              cell.SetCellValue("");
            }
            //里程客流票款统计(月度)管理
            cell = row.CreateCell(8);
            cell.CellStyle = TextSytle;
            if (item.authoritys.Any(o => o.Contains("里程客流票款统计(月度)管理")))
            {
              cell.SetCellValue("√");
            }
            else
            {
              cell.SetCellValue("");
            }
            //停车场(半年)管理
            cell = row.CreateCell(9);
            cell.CellStyle = TextSytle;
            if (item.authoritys.Any(o => o.Contains("停车场(半年)管理")))
            {
              cell.SetCellValue("√");
            }
            else
            {
              cell.SetCellValue("");
            }
            //维护保养场(半年)管理
            cell = row.CreateCell(10);
            cell.CellStyle = TextSytle;
            if (item.authoritys.Any(o => o.Contains("维护保养场(半年)管理")))
            {
              cell.SetCellValue("√");
            }
            else
            {
              cell.SetCellValue("");
            }
            //调度中心(半年)管理
            cell = row.CreateCell(11);
            cell.CellStyle = TextSytle;
            if (item.authoritys.Any(o => o.Contains("调度中心(半年)管理")))
            {
              cell.SetCellValue("√");
            }
            else
            {
              cell.SetCellValue("");
            }
            //里程客流票款汇总
            cell = row.CreateCell(12);
            cell.CellStyle = TextSytle;
            if (item.authoritys.Any(o => o.Contains("里程客流票款汇总")))
            {
              cell.SetCellValue("√");
            }
            else
            {
              cell.SetCellValue("");
            }
            //报送工作人员联络清单管理
            cell = row.CreateCell(13);
            cell.CellStyle = TextSytle;
            if (item.authoritys.Any(o => o.Contains("报送工作人员联络清单管理")))
            {
              cell.SetCellValue("√");
            }
            else
            {
              cell.SetCellValue("");
            }
            //公共交通统计表
            cell = row.CreateCell(14);
            cell.CellStyle = TextSytle;
            if (item.authoritys.Any(o => o.Contains("公共交通统计表")))
            {
              cell.SetCellValue("√");
            }
            else
            {
              cell.SetCellValue("");
            }
            //客运统计
            cell = row.CreateCell(15);
            cell.CellStyle = TextSytle;
            if (item.authoritys.Any(o => o.Contains("客运统计")))
            {
              cell.SetCellValue("√");
            }
            else
            {
              cell.SetCellValue("");
            }
            //便民车统计
            cell = row.CreateCell(16);
            cell.CellStyle = TextSytle;
            if (item.authoritys.Any(o => o.Contains("便民车统计")))
            {
              cell.SetCellValue("√");
            }
            else
            {
              cell.SetCellValue("");
            }
            //楼巴统计
            cell = row.CreateCell(17);
            cell.CellStyle = TextSytle;
            if (item.authoritys.Any(o => o.Contains("楼巴统计")))
            {
              cell.SetCellValue("√");
            }
            else
            {
              cell.SetCellValue("");
            }
            //线路经营权管理
            cell = row.CreateCell(18);
            cell.CellStyle = TextSytle;
            if (item.authoritys.Any(o => o.Contains("线路经营权管理")))
            {
              cell.SetCellValue("√");
            }
            else
            {
              cell.SetCellValue("");
            }
            //公交线路、驾驶员分级分类表(季度)
            cell = row.CreateCell(19);
            cell.CellStyle = TextSytle;
            if (item.authoritys.Any(o => o.Contains("公交线路、驾驶员分级分类表(季度)")))
            {
              cell.SetCellValue("√");
            }
            else
            {
              cell.SetCellValue("");
            }
            //司机信息表(年度)
            cell = row.CreateCell(20);
            cell.CellStyle = TextSytle;
            if (item.authoritys.Any(o => o.Contains("司机信息表(年度)")))
            {
              cell.SetCellValue("√");
            }
            else
            {
              cell.SetCellValue("");
            }
            //每日检查表(半月表)
            cell = row.CreateCell(21);
            cell.CellStyle = TextSytle;
            if (item.authoritys.Any(o => o.Contains("每日检查表(半月表)")))
            {
              cell.SetCellValue("√");
            }
            else
            {
              cell.SetCellValue("");
            }
            //双层巴士驾驶员信息库
            cell = row.CreateCell(22);
            cell.CellStyle = TextSytle;
            if (item.authoritys.Any(o => o.Contains("双层巴士驾驶员信息库")))
            {
              cell.SetCellValue("√");
            }
            else
            {
              cell.SetCellValue("");
            }
            //双层巴士车辆信息库
            cell = row.CreateCell(23);
            cell.CellStyle = TextSytle;
            if (item.authoritys.Any(o => o.Contains("双层巴士车辆信息库")))
            {
              cell.SetCellValue("√");
            }
            else
            {
              cell.SetCellValue("");
            }
            //双层巴士线路信息库
            cell = row.CreateCell(24);
            cell.CellStyle = TextSytle;
            if (item.authoritys.Any(o => o.Contains("双层巴士线路信息库")))
            {
              cell.SetCellValue("√");
            }
            else
            {
              cell.SetCellValue("");
            }
            //月度安全运行情况
            cell = row.CreateCell(25);
            cell.CellStyle = TextSytle;
            if (item.authoritys.Any(o => o.Contains("月度安全运行情况")))
            {
              cell.SetCellValue("√");
            }
            else
            {
              cell.SetCellValue("");
            }
            //季度安全运行情况
            cell = row.CreateCell(26);
            cell.CellStyle = TextSytle;
            if (item.authoritys.Any(o => o.Contains("季度安全运行情况")))
            {
              cell.SetCellValue("√");
            }
            else
            {
              cell.SetCellValue("");
            }
            //双层巴士线路预警信息管理
            cell = row.CreateCell(27);
            cell.CellStyle = TextSytle;
            if (item.authoritys.Any(o => o.Contains("双层巴士线路预警信息管理")))
            {
              cell.SetCellValue("√");
            }
            else
            {
              cell.SetCellValue("");
            }
            //日驾驶超12小时明细
            cell = row.CreateCell(28);
            cell.CellStyle = TextSytle;
            if (item.authoritys.Any(o => o.Contains("日驾驶超12小时明细")))
            {
              cell.SetCellValue("√");
            }
            else
            {
              cell.SetCellValue("");
            }
            //连续驾驶超8小时明细
            cell = row.CreateCell(29);
            cell.CellStyle = TextSytle;
            if (item.authoritys.Any(o => o.Contains("连续驾驶超8小时明细")))
            {
              cell.SetCellValue("√");
            }
            else
            {
              cell.SetCellValue("");
            }
            //月驾驶超203小时明细
            cell = row.CreateCell(30);
            cell.CellStyle = TextSytle;
            if (item.authoritys.Any(o => o.Contains("月驾驶超203小时明细")))
            {
              cell.SetCellValue("√");
            }
            else
            {
              cell.SetCellValue("");
            }
            //连续工作超过6天
            cell = row.CreateCell(31);
            cell.CellStyle = TextSytle;
            if (item.authoritys.Any(o => o.Contains("连续工作超过6天")))
            {
              cell.SetCellValue("√");
            }
            else
            {
              cell.SetCellValue("");
            }
            sIndex++;
          }
        },
        out msg,
        3,
        true
      );
      return ms;
    }
  }
}
